package com.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.dao.CarouselMapper;
import com.web.dao.GoodsMapper;
import com.web.dao.MenuMapper;
import com.web.dao.StoreMapper;
import com.web.domain.ApiResponse;
import com.web.dto.IndexQueryDto;
import com.web.dto.StoreDto;
import com.web.entity.Carousel;
import com.web.entity.Store;
import com.web.manager.GaoDeManager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FDjavaone
 * @date 2017/8/31
 */

@RestController
@RequestMapping("api")
public class IndexController {

    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private MenuMapper menuMapper;

    /**
     * app首页数据
     */
    @RequestMapping("app/index")
    public ApiResponse index(IndexQueryDto indexQueryDto) {
        ApiResponse response = new ApiResponse();
        Store store = new Store();
        store = getAddress(indexQueryDto, store);
        store.setId(indexQueryDto.getId());
        int pageNum = 1;
        int pageSize = 40;
        if (indexQueryDto.getPageNum() != null) {
            pageNum = indexQueryDto.getPageNum();
        }
        if (indexQueryDto.getPageSize() != null) {
            pageSize = indexQueryDto.getPageSize();
        }
        if (indexQueryDto.getId() == null) {
            store.setId(0L);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<StoreDto> stores = storeMapper.selectStoreAround(store);
        PageInfo page = new PageInfo(stores);
        //获取真正启用中的轮播
        List<Carousel> carousels = carouselMapper.selectActivity();
        Map map = new HashMap();
        map.put("stores", stores);
        map.put("total", page.getTotal());
        map.put("carousels", carousels);

        response.addParams(map);
        response.setMessage("获取成功!");
        response.setStatus(900000);
        return response;
    }

    /**
     * app首页搜索
     */
    @RequestMapping("app/search")
    public ApiResponse search(IndexQueryDto indexQueryDto) {
        ApiResponse response = new ApiResponse();
        //含有关键词的商品所属的菜单id
        List<Long> menus = goodsMapper.selectByTitle("%" + indexQueryDto.getKeyword() + "%");
        if (menus.size() == 0) {
            menus.add(0L);
        }

        List<Long> storesId = menuMapper.selectStoreIdByMenuId(menus);
        storesId.add(0L);
        storesId = storeMapper.fitterOnBusinessStore(storesId);
        if (storesId.size() == 0) {
            storesId.add(0L);
        }

        Store store = new Store();
        store.setMerchantName("%" + indexQueryDto.getKeyword() + "%");
        store.setId(indexQueryDto.getId());
        store = getAddress(indexQueryDto, store);
        List<StoreDto> stores;
        PageHelper.startPage(indexQueryDto.getPageNum(), indexQueryDto.getPageSize());
        if (indexQueryDto.getSort() == 1) {
            //按照距离排序
            stores = storeMapper.selectStoreAroundAndByAddress(store, storesId);
        } else {
            //按星级排序
            stores = storeMapper.selectStoreAroundAndByLevel(store, storesId);
        }
        PageInfo page = new PageInfo(stores);
        Map map = new HashMap();
        map.put("stores", stores);
        map.put("total", page.getTotal());

        response.addParams(map);
        response.setMessage("获取成功!");
        response.setStatus(900000);
        return response;
    }

    /**
     * app首页按类别关键词搜索
     */
    @RequestMapping("app/searchByCategory")
    public ApiResponse searchByCategory(IndexQueryDto indexQueryDto) {
        ApiResponse response = new ApiResponse();
        Store store = new Store();
        store.setId(indexQueryDto.getId());
        List<StoreDto> stores;
        if ("附近".equals(indexQueryDto.getType())) {
            //搜索附近的店铺按距离远近或星级
            if (indexQueryDto.getKeyword() != null && !"".equals(indexQueryDto.getKeyword())) {
                //含有关键词的商品所属的菜单id
                List<Long> menus = goodsMapper.selectByTitle("%" + indexQueryDto.getKeyword() + "%");
                if (menus.size() == 0) {
                    menus.add(0L);
                }
                List<Long> storesId = menuMapper.selectStoreIdByMenuId(menus);
                storesId.add(0L);
                storesId = storeMapper.fitterOnBusinessStore(storesId);
                if (storesId.size() == 0) {
                    storesId.add(0L);
                }
                //含有关键词
                store.setMerchantName("%" + indexQueryDto.getKeyword() + "%");
                store = getAddress(indexQueryDto, store);
                PageHelper.startPage(indexQueryDto.getPageNum(), indexQueryDto.getPageSize());
                if (indexQueryDto.getSort() == 1) {
                    //按照距离排序
                    stores = storeMapper.selectStoreAroundAndByAddress(store, storesId);
                } else {
                    //按星级排序
                    stores = storeMapper.selectStoreAroundAndByLevel(store, storesId);
                }
            } else {
                //不含有关键词
                store = getAddress(indexQueryDto, store);
                PageHelper.startPage(indexQueryDto.getPageNum(), indexQueryDto.getPageSize());
                if (indexQueryDto.getSort() == 1) {
                    //按照距离排序
                    stores = storeMapper.selectStoreAroundAndByAddressNoKeyword(store);
                } else {
                    //按星级排序
                    stores = storeMapper.selectStoreAroundAndByLevelNoKeyword(store);
                }
            }
        } else {
            //正常类别
            if (indexQueryDto.getKeyword() != null && !"".equals(indexQueryDto.getKeyword())) {
                //含有关键词的商品所属的菜单id
                List<Long> menus = goodsMapper.selectByTitle("%" + indexQueryDto.getKeyword() + "%");
                if (menus.size() == 0) {
                    menus.add(0L);
                }
                List<Long> storesId = menuMapper.selectStoreIdByMenuId(menus);
                storesId.add(0L);
                storesId = storeMapper.fitterOnBusinessStore(storesId);
                if (storesId.size() == 0) {
                    storesId.add(0L);
                }
                //含有关键词
                store.setMerchantName("%" + indexQueryDto.getKeyword() + "%");
                store = getAddress(indexQueryDto, store);
                store.setType(indexQueryDto.getType());
                PageHelper.startPage(indexQueryDto.getPageNum(), indexQueryDto.getPageSize());
                if (indexQueryDto.getSort() == 1) {
                    //按照距离排序
                    stores = storeMapper.selectStoreAroundAndByAddressAndType(store, storesId);
                } else {
                    //按星级排序
                    stores = storeMapper.selectStoreAroundAndByLevelAndType(store, storesId);
                }
            } else {
                //不含有关键词
                store = getAddress(indexQueryDto, store);
                store.setType(indexQueryDto.getType());
                PageHelper.startPage(indexQueryDto.getPageNum(), indexQueryDto.getPageSize());
                if (indexQueryDto.getSort() == 1) {
                    //按照距离排序
                    stores = storeMapper.selectStoreByTypeAndNoKeywordOrderByDistance(store);
                } else {
                    //按星级排序
                    stores = storeMapper.selectStoreByTypeAndNoKeywordOrderByLevel(store);
                }
            }
        }


        PageInfo page = new PageInfo(stores);
        Map map = new HashMap();
        map.put("stores", stores);
        map.put("total", page.getTotal());

        response.addParams(map);
        response.setMessage("获取成功!");
        response.setStatus(900000);
        return response;
    }

    /**
     * 获取用户的地理位置信息
     */
    public Store getAddress(IndexQueryDto indexQueryDto, Store store) {
        BeanUtils.copyProperties(indexQueryDto, store);
        if (indexQueryDto.getLoc() != null) {
            //坐标存在，使用坐标获取附近的店铺列表
            store.setLat(new Double(indexQueryDto.getLoc().split(",")[1]));
            store.setLng(new Double(indexQueryDto.getLoc().split(",")[0]));
        } else {
            //坐标不存在，使用省市区获取店铺店铺列表
            ApiResponse locResponse = GaoDeManager.transferAddress(indexQueryDto.getProvince() + indexQueryDto.getCity() + indexQueryDto.getRegion());
            if (locResponse.getStatus() == 900000) {
                String loc = (String) locResponse.getParams();
                store.setLat(new Double(loc.split(",")[1]));
                store.setLng(new Double(loc.split(",")[0]));
            }
        }
        return store;
    }

}


